﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO.Model;
using DAO.Bussiness.Repository;

namespace new_Seminario.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

            Roles.AddUserToRole(RegisterUser.UserName, "Cliente");
            
            Usuario user = new Usuario();
            UsuarioRepository repo = new UsuarioRepository();

            
            user.Apellido = ((TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("txtApellido")).Text.Trim();
            user.Nombre = ((TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("txtNombre")).Text.Trim();
            user.Id_Tipo_Doc = Convert.ToInt16(((DropDownList)RegisterUserWizardStep.ContentTemplateContainer.FindControl("ddlTipo")).SelectedItem.Value);
            // HARDCODEADO PARA QUE GENERE EL ID_ROL CLIENTE
            user.Id_Rol = 2;
            user.Nro_Doc = ((TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("txtDni")).Text.Trim();
            user.Fecha_Ingreso = DateTime.Now;
            user.Password = ((TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Password")).Text.Trim();
            user.Direccion = RegisterUser.UserName;

            repo.Add(user);
            repo.Save();
            
            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }
            Response.Redirect(continueUrl);
        }

    }
}
